package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.BrandAdapater;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.BrandInfoBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.SideBar;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/5/23 0023
 */
public class CarType1Activity extends BaseActivity {
    @BindView(R.id.cartype_head)
    ExpandHeadView mCartypeHead;
    @BindView(R.id.lv_brand)
    ListView mLvBrand;
    @BindView(R.id.side_bar)
    SideBar mSideBar;
    public BrandInfoBean mBrandInfoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cartype;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initListener() {
        //返回键
        mCartypeHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSideBar.setOnStrSelectCallBack(new SideBar.ISideBarSelectCallBack() {
            @Override
            public void onSelectStr(int index, String selectStr) {
                for (int i = 0; i <mBrandInfoBean.getRows().size(); i++) {
                    if (selectStr.equalsIgnoreCase(mBrandInfoBean.getRows().get(i).getVcFirst())) {
                        mLvBrand.setSelection(i); // 选择到首字母出现的位置
                        return;
                    }
                }
            }
        });
        mLvBrand.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BrandInfoBean.RowsBean rowsBean = mBrandInfoBean.getRows().get(position);
                String vcBrand = rowsBean.getVcBrand();
                Intent intent= new Intent(UIUtils.getContext(),CarType2Activity.class);
                intent.putExtra("vcBrand",vcBrand);
                intent.putExtra("firstid",rowsBean.getId());
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        try{
            OkHttpUtils.post().url(Constants.BRANTYPEURL).addParams("data","1").addParams("sign",Constants.SIGN).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyToast.show("数据加载失败,请检查网络...");
                    ALog.d("请求失败,显示失败视图");
                }

                @Override
                public void onResponse(String response, int id) {
                    mSideBar.setVisibility(View.VISIBLE);
                 //   ALog.d(response);
                    Gson gson = new Gson();
                    mBrandInfoBean = gson.fromJson(response, BrandInfoBean.class);
                    mLvBrand.setAdapter(new BrandAdapater(mBrandInfoBean.getRows()));
                    ALog.d(mBrandInfoBean.toString());
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
